package com.example.group.network.express_plus_app.viewmodel;

import android.app.Application;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.group.network.express_plus_app.R;
import com.example.group.network.express_plus_app.bean.Order;
import com.example.group.network.express_plus_app.http.GsonArrayRequest;
import com.example.group.network.express_plus_app.http.MySingleton;

import java.util.ArrayList;

/**
 * @author 夏宁
 * @email 1941512303@qq.com
 * @date 2021/4/4 23:10
 */
public class SearchViewModel extends AndroidViewModel {
    String server_ip ;
    private MutableLiveData<ArrayList<Order>> orderMutableLiveData;
    RequestQueue queue;
    public SearchViewModel(@NonNull Application application) {
        super(application);
        orderMutableLiveData = new MutableLiveData<ArrayList<Order>>();
        queue= MySingleton.getInstance(application).getRequestQueue();
        server_ip=getApplication().getString(R.string.server_ip);
    }
    public LiveData<ArrayList<Order>> getText() {
        return orderMutableLiveData;
    }
    //根据搜索内容，搜索标签搜索订单
    public void findOrderBySearchAndParam(
            String searchContent,String param){

        String url=server_ip+"express-order/open-order/find-order-by-search?searchContent="
                +searchContent+"&param="+param;
        System.out.println(server_ip);
        GsonArrayRequest gsonArrayRequest=new GsonArrayRequest(url, Order.class, null, new Response.Listener<ArrayList<Order>>() {
            @Override
            public void onResponse(ArrayList<Order> response) {
                orderMutableLiveData.setValue(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.i("出错", String.valueOf(error.getNetworkTimeMs()));
            }
        });

        gsonArrayRequest.setRetryPolicy(new DefaultRetryPolicy(6000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(gsonArrayRequest);

    }

}
